//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls 
{
    // *********************************************************************
    //  ThreadsMove
    //
    /// <summary>
    /// This sever control is used to move a thread from forum to forum.
    /// </summary>
    /// 
    // ********************************************************************/ 
    public class ThreadsMove : TemplatedWebControl
    {
        #region Child Controls

        ForumListBox moveTo;
        CheckBox sendEmail;
        IButton cancelMove;
        IButton move;
        RequiredFieldValidator moveToValidator;

        string[] threadsToMove;
        CSContext csContext = CSContext.Current;

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "Moderation/View-ThreadsMove.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            if (csContext.ReturnUrl == null) 
            {
                throw new CSException(CSExceptionType.ReturnURLRequired);
            }

            // Get the post we want to move
            //
            threadsToMove = HttpContext.Current.Request.QueryString["Threads"].Split(Convert.ToChar(","));

            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            if (!Page.IsPostBack)
                DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();
            
            if (null != cancelMove) 
                cancelMove.Text = ResourceManager.GetString("MovePost_CancelMove");

            if (null != move) 
                move.Text = ResourceManager.GetString("MovePost_MovePost");

            if (null != sendEmail)
                sendEmail.Text = ResourceManager.GetString("MovePost_SendUserEmail");
        }
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {           
            moveTo = (ForumListBox) FindControl("MoveTo");
            cancelMove = FindButton("CancelMove");
            move = FindButton("MovePost");
            moveToValidator = (RequiredFieldValidator) FindControl("MoveToValidator");
            sendEmail = (CheckBox) FindControl("SendUserEmail");
			
            InitializeChildControls();
        }

        private void InitializeChildControls() 
        {
            if (null != cancelMove) 
                cancelMove.Click += new EventHandler(Cancel_Click);

            if (null != move) 
                move.Click += new System.EventHandler(MoveThread_Click);
        }
		        
        #endregion

        #region Event Handlers

        // *********************************************************************
        //  MoveThread_Click
        //
        /// <summary>
        /// Event handler for deleting a post
        /// </summary>
        /// 
        // ***********************************************************************/
        private void MoveThread_Click(Object sender, EventArgs e) 
        {
            // Make sure their selection is a valid forum.
            if (moveTo.SelectedForum == 0)
            {
                moveToValidator.IsValid = false;
                return;
            }

            // Are we valid?
            //
            for (int i = 0; i < threadsToMove.Length; i++)
            {
                ForumPost postToMove = Posts.GetPost(Convert.ToInt32(threadsToMove[i]), csContext.User.UserID);

                // Do an access check
                Permissions.AccessCheck(postToMove.Section, Permission.Moderate, csContext.User, postToMove );

                Moderate.MovePost(postToMove, moveTo.SelectedForum, csContext.User.UserID, sendEmail.Checked);
            }
            
            // Remove cached group forums list
            //
            CSCache.RemoveByPattern( string.Format( "Forums-Site:{0}", csContext.SiteSettings.SettingsID ) );

            // Redirect the user to the return url.
            HttpContext.Current.Response.Redirect(csContext.ReturnUrl);
            HttpContext.Current.Response.End();
        }

        void Cancel_Click(object sender, EventArgs e) 
        {
            moveToValidator.IsValid = true;

            CSContext.Current.Context.Response.Redirect(csContext.ReturnUrl, true);
        }

        #endregion
    }
}
